<template><div><h2 id="重置密码" tabindex="-1"><a class="header-anchor" href="#重置密码"><span>重置密码</span></a></h2>
<ul>
<li><a href="#introduction">介绍</a>
<ul>
<li><a href="#model-preparation">模型准备</a></li>
<li><a href="#database-preparation">数据库准备</a></li>
<li><a href="#configuring-trusted-hosts">配置可信主机</a></li>
</ul>
</li>
<li><a href="#routing">路由</a>
<ul>
<li><a href="#requesting-the-password-reset-link">请求密码重置链接</a></li>
<li><a href="#resetting-the-password">重置密码</a></li>
</ul>
</li>
<li><a href="#deleting-expired-tokens">删除过期令牌</a></li>
<li><a href="#password-customization">自定义</a></li>
</ul>
<h2 id="介绍" tabindex="-1"><a class="header-anchor" href="#介绍"><span>介绍</span></a></h2>
<p>大多数 Web 应用程序都提供了一种让用户重置密码的方法。Laravel 已经提供了便捷的服务来发送密码重置链接和安全重置密码，而不需要您为每个应用程序重新实现此功能。</p>
<blockquote>
<p><strong>注意</strong><br>
想要快速入门吗？在全新的 Laravel 应用程序中安装 Laravel <a href="https://learnku.com/docs/laravel/10.x/starter-kits" target="_blank" rel="noopener noreferrer">入门套件</a>。 Laravel 的起始包将为您的整个身份验证系统包括重置忘记的密码提供支持。</p>
</blockquote>
<h3 id="模型准备" tabindex="-1"><a class="header-anchor" href="#模型准备"><span>模型准备</span></a></h3>
<p>在使用 Laravel 的密码重置功能之前，您的应用程序的 <code v-pre>App\Models\User</code> 模型必须使用 <code v-pre>Illuminate\Notifications\Notifiable</code> trait。通常，在新创建的 Laravel 应用程序的 <code v-pre>App\Models\User</code> 模型中默认引入了该 trait 。</p>
<p>接下来，验证您的 <code v-pre>App\Models\User</code> 模型是否实现了 <code v-pre>Illuminate\Contracts\Auth\CanResetPassword</code> 契约。框架中包含的 <code v-pre>App\Models\User</code> 模型已经实现了该接口，并使用 <code v-pre>Illuminate\Auth\Passwords\CanResetPassword</code> 特性来包括实现该接口所需的方法。</p>
<h3 id="数据库准备" tabindex="-1"><a class="header-anchor" href="#数据库准备"><span>数据库准备</span></a></h3>
<p>必须创建一个表来存储您的应用程序的密码重置令牌。这个表的迁移被包含在默认的 Laravel 应用程序中，所以您只需要迁移您的数据库来创建这个表：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">php artisan migrate</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h3 id="配置受信任的主机" tabindex="-1"><a class="header-anchor" href="#配置受信任的主机"><span>配置受信任的主机</span></a></h3>
<p>默认情况下，无论 HTTP 请求的 <code v-pre>Host</code> 头的内容是什么，Laravel 都会响应它收到的所有请求。此外，在 Web 请求期间生成应用程序的绝对 URL 时，将使用 <code v-pre>Host</code> 标头的值。</p>
<p>通常，您应该将 Web 服务器（例如 Nginx 或 Apache）配置为仅向您的应用程序发送与给定主机名匹配的请求。然而，如果你没有能力直接自定义你的 web 服务器并且需要指示 Laravel 只响应某些主机名，你可以通过为你的应用程序启用 <code v-pre>App\Http\Middleware\TrustHosts</code> 中间件来实现。当您的应用程序提供密码重置功能时，这一点尤其重要.</p>
<p>要了解有关此中间件的更多信息，请参阅<a href="https://learnku.com/docs/laravel/10.x/requests#configuring-trusted-hosts" target="_blank" rel="noopener noreferrer"><code v-pre>TrustHosts</code> 中间件文档</a>。</p>
<h2 id="路由" tabindex="-1"><a class="header-anchor" href="#路由"><span>路由</span></a></h2>
<p>要正确实现支持允许用户重置其密码的功能，我们需要定义多个路由。首先，我们需要一对路由来处理允许用户通过其电子邮件地址请求密码重置链接。其次，一旦用户访问通过电子邮件发送给他们的密码重置链接并完成密码重置表单，我们将需要一对路由来处理实际重置密码。</p>
<h3 id="请求密码重置链接" tabindex="-1"><a class="header-anchor" href="#请求密码重置链接"><span>请求密码重置链接</span></a></h3>
<h4 id="密码重置链接申请表" tabindex="-1"><a class="header-anchor" href="#密码重置链接申请表"><span>密码重置链接申请表</span></a></h4>
<p>首先，我们将定义请求密码重置链接所需的路由。首先，我们将定义一个路由，该路由返回一个带有密码重置链接请求表单的视图：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Route</span><span class="token operator">::</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'/forgot-password'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token function">view</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'auth.forgot-password'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">middleware</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'guest'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">name</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'password.request'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>此路由返回的视图应该有一个包含 <code v-pre>email</code> 字段的表单，该字段允许用户请求给定电子邮件地址的密码重置链接。</p>
<h4 id="处理表单提交" tabindex="-1"><a class="header-anchor" href="#处理表单提交"><span>处理表单提交</span></a></h4>
<p>接下来，我们将定义一个路由，该路由将从「忘记密码」视图处理表单提交请求。此路由将负责验证电子邮件地址并将密码重置请求发送给相应用户：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Http<span class="token punctuation">\</span>Request</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>Password</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">Route</span><span class="token operator">::</span><span class="token function">post</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'/forgot-password'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">Request</span> <span class="token variable">$request</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token variable">$request</span><span class="token operator">-></span><span class="token function">validate</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'email'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'required|email'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$status</span> <span class="token operator">=</span> <span class="token class-name static-context">Password</span><span class="token operator">::</span><span class="token function">sendResetLink</span><span class="token punctuation">(</span></span>
<span class="line">        <span class="token variable">$request</span><span class="token operator">-></span><span class="token function">only</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'email'</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token keyword">return</span> <span class="token variable">$status</span> <span class="token operator">===</span> <span class="token class-name static-context">Password</span><span class="token operator">::</span><span class="token constant">RESET_LINK_SENT</span></span>
<span class="line">                <span class="token operator">?</span> <span class="token function">back</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">with</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'status'</span> <span class="token operator">=></span> <span class="token function">__</span><span class="token punctuation">(</span><span class="token variable">$status</span><span class="token punctuation">)</span><span class="token punctuation">]</span><span class="token punctuation">)</span></span>
<span class="line">                <span class="token punctuation">:</span> <span class="token class-name return-type">back</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">withErrors</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'email'</span> <span class="token operator">=></span> <span class="token function">__</span><span class="token punctuation">(</span><span class="token variable">$status</span><span class="token punctuation">)</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">middleware</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'guest'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">name</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'password.email'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>在继续之前，让我们更详细地检查一下这条路由。首先，验证请求的 <code v-pre>email</code> 属性。接下来，我们将使用 Laravel 内置的 <code v-pre>Password</code> 门面向用户发送一个密码重置链接。密码代理将负责按给定字段（在本例中是电子邮件地址）检索用户，并通过 Laravel 的内置 <a href="https://learnku.com/docs/laravel/10.x/notifications" target="_blank" rel="noopener noreferrer">消息通知系统</a> 向用户发送密码重置链接。</p>
<p>该 <code v-pre>sendResetLink</code> 方法返回一个状态标识。可以使用 Laravel 的 <a href="https://learnku.com/docs/laravel/10.x/localization" target="_blank" rel="noopener noreferrer">本地化</a> 助手来转换此状态，以便向用户显示有关请求状态的用户友好提示。密码重置状态的转换由应用程序的 <code v-pre>lang/{lang}/passwords.php</code> 语言文件决定。状态 slug 的每个可能值的条目位于 <code v-pre>passwords</code> 语言文件中。</p>
<blockquote>
<p><strong>注意</strong><br>
默认情况下, Laravel应用程序的框架不包括 <code v-pre>lang</code> 目录. 如果你想定制Laravel的语言文件, 你可以通过 <code v-pre>lang:publish</code> Artisan命令发布.</p>
</blockquote>
<p>你可能想知道，Laravel 在调用 Password 门面的 sendResetLink 方法时，Laravel 怎么知道如何从应用程序数据库中检索用户记录。Laravel 密码代理利用身份验证系统的「用户提供者」来检索数据库记录。密码代理使用的用户提供程序是在 passwords 配置文件的 config/auth.php 配置数组中配置的。要了解有关编写自定义用户提供程序的更多信息，请参阅 <a href="https://learnku.com/docs/laravel/10.x/authenticationmd#adding-custom-user-providers" target="_blank" rel="noopener noreferrer">身份验证文档</a>。</p>
<blockquote>
<p><strong>Note</strong><br>
技巧：当手动实现密码重置时，你需要自己定义视图和路由的内容。如果你想要包含所有必要的身份验证和验证逻辑的脚手架，请查看 <a href="https://learnku.com/docs/laravel/10.x/starter-kits" target="_blank" rel="noopener noreferrer">Laravel 应用程序入门工具包</a>。</p>
</blockquote>
<h3 id="重置密码-1" tabindex="-1"><a class="header-anchor" href="#重置密码-1"><span>重置密码</span></a></h3>
<h4 id="重置密码表单" tabindex="-1"><a class="header-anchor" href="#重置密码表单"><span>重置密码表单</span></a></h4>
<p>接下来，我们将定义用户点击重置密码邮件中的链接，进行重置密码所需要的一些路由。第一步，先定义一个获取重置密码表单的路由。这个路由需要一个 <code v-pre>token</code> 来验证请求：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Route</span><span class="token operator">::</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'/reset-password/{token}'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token keyword type-hint">string</span> <span class="token variable">$token</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token function">view</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'auth.reset-password'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'token'</span> <span class="token operator">=></span> <span class="token variable">$token</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">middleware</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'guest'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">name</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'password.reset'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>通过路由返回的视图应该显示一个含有 <code v-pre>email</code> 字段， <code v-pre>password</code> 字段，<code v-pre>password_confirmation</code> 字段和一个隐藏的值通过路由参数获取的 <code v-pre>token</code> 字段。</p>
<h4 id="处理表单提交的数据" tabindex="-1"><a class="header-anchor" href="#处理表单提交的数据"><span>处理表单提交的数据</span></a></h4>
<p>当然，我们需要定义一个路由来接受表单提交的数据。这个路由会检查传过来的参数并更新数据库中用户的密码：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">App<span class="token punctuation">\</span>Models<span class="token punctuation">\</span>User</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Auth<span class="token punctuation">\</span>Events<span class="token punctuation">\</span>PasswordReset</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Http<span class="token punctuation">\</span>Request</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>Hash</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>Password</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">Route</span><span class="token operator">::</span><span class="token function">post</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'/reset-password'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">Request</span> <span class="token variable">$request</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token variable">$request</span><span class="token operator">-></span><span class="token function">validate</span><span class="token punctuation">(</span><span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'token'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'required'</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token string single-quoted-string">'email'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'required|email'</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token string single-quoted-string">'password'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'required|min:8|confirmed'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$status</span> <span class="token operator">=</span> <span class="token class-name static-context">Password</span><span class="token operator">::</span><span class="token function">reset</span><span class="token punctuation">(</span></span>
<span class="line">        <span class="token variable">$request</span><span class="token operator">-></span><span class="token function">only</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'email'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'password'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'password_confirmation'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'token'</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">User</span> <span class="token variable">$user</span><span class="token punctuation">,</span> <span class="token keyword type-hint">string</span> <span class="token variable">$password</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">            <span class="token variable">$user</span><span class="token operator">-></span><span class="token function">forceFill</span><span class="token punctuation">(</span><span class="token punctuation">[</span></span>
<span class="line">                <span class="token string single-quoted-string">'password'</span> <span class="token operator">=></span> <span class="token class-name static-context">Hash</span><span class="token operator">::</span><span class="token function">make</span><span class="token punctuation">(</span><span class="token variable">$password</span><span class="token punctuation">)</span></span>
<span class="line">            <span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">setRememberToken</span><span class="token punctuation">(</span><span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">random</span><span class="token punctuation">(</span><span class="token number">60</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">            <span class="token variable">$user</span><span class="token operator">-></span><span class="token function">save</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">            <span class="token function">event</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">PasswordReset</span><span class="token punctuation">(</span><span class="token variable">$user</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">        <span class="token punctuation">}</span></span>
<span class="line">    <span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token keyword">return</span> <span class="token variable">$status</span> <span class="token operator">===</span> <span class="token class-name static-context">Password</span><span class="token operator">::</span><span class="token constant">PASSWORD_RESET</span></span>
<span class="line">                <span class="token operator">?</span> <span class="token function">redirect</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">route</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'login'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">with</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'status'</span><span class="token punctuation">,</span> <span class="token function">__</span><span class="token punctuation">(</span><span class="token variable">$status</span><span class="token punctuation">)</span><span class="token punctuation">)</span></span>
<span class="line">                <span class="token punctuation">:</span> <span class="token class-name return-type">back</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">withErrors</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'email'</span> <span class="token operator">=></span> <span class="token punctuation">[</span><span class="token function">__</span><span class="token punctuation">(</span><span class="token variable">$status</span><span class="token punctuation">)</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">middleware</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'guest'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">name</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'password.update'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>在继续之前，我们再详细地检查下这条路由。 首先，验证请求的 <code v-pre>token</code>，<code v-pre>email</code> 和 <code v-pre>password</code> 属性。 接下来，我们将使用 Laravel 的内置「密码代理」 (通过 <code v-pre>Password</code> facade) 来验证密码重置请求凭据。</p>
<p>如果提供给密码代理的令牌、电子邮件地址和密码有效，则将调用传递给 <code v-pre>reset</code> 方法的闭包。 在这个接收用户实例和纯文本密码的闭包中，我们可以更新数据库中用户的密码。</p>
<p>该 <code v-pre>reset</code> 方法返回一个「状态」标识。 此状态可以使用 Laravel 的 <a href="https://learnku.com/docs/laravel/10.x/localization" target="_blank" rel="noopener noreferrer">本地化</a> 助手来翻译此状态，以便向用户显示有关其请求状态的用户友好消息。密码重置状态的翻译由应用程序的 <code v-pre>lang/{lang}/passwords.php</code> 语言文件决定。状态段的每个可能值的条目位于 <code v-pre>passwords</code> 语言文件中。如果你的应用没有 <code v-pre>lang</code> 文件夹，你可以使用 <code v-pre>lang:publish</code> artisan命令来创建。</p>
<p>在继续之前，你可能想知道 Laravel 如何在调用 <code v-pre>Password</code> facade的 <code v-pre>reset</code> 方法时如何知道如何从应用程序的数据库中检索用户记录。Laravel 密码代理利用你的身份验证系统的「用户提供者」来检索数据库记录。密码代理使用的用户提供程序在配置文件的 <code v-pre>config/auth.php</code> 配置文件的 <code v-pre>passwords</code> 配置数组中配置。 要了解有关编写自定义用户提供程序的更多信息，请参阅 <a href="https://learnku.com/docs/laravel/10.x/authenticationmd#adding-custom-user-providers" target="_blank" rel="noopener noreferrer">身份验证文档</a>。</p>
<h2 id="删除过期令牌" tabindex="-1"><a class="header-anchor" href="#删除过期令牌"><span>删除过期令牌</span></a></h2>
<p>已过期的密码重置令牌仍将存在于你的数据库中。然而，你可以使用 <code v-pre>auth:clear-resets</code> Artisan 命令轻松删除这些记录：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">php artisan auth:clear-resets</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>如果你想使该过程自动化，请考虑将命令添加到应用程序的 <a href="https://learnku.com/docs/laravel/10.x/scheduling" target="_blank" rel="noopener noreferrer">调度程序</a>：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$schedule</span><span class="token operator">-></span><span class="token function">command</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'auth:clear-resets'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">everyFifteenMinutes</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h2 id="自定义" tabindex="-1"><a class="header-anchor" href="#自定义"><span>自定义</span></a></h2>
<h4 id="重置链接自定义" tabindex="-1"><a class="header-anchor" href="#重置链接自定义"><span>重置链接自定义</span></a></h4>
<p>你可以使用 <code v-pre>ResetPassword</code> 通知类提供的 <code v-pre>createUrlUsing</code> 方法自定义密码重置链接 URL。 此方法接受一个闭包，该闭包接收正在接收通知的用户实例以及密码重置链接令牌。 通常，你应该从 <code v-pre>App\Providers\AuthServiceProvider</code> 服务提供者的 <code v-pre>boot</code> 方法中调用此方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">App<span class="token punctuation">\</span>Models<span class="token punctuation">\</span>User</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Auth<span class="token punctuation">\</span>Notifications<span class="token punctuation">\</span>ResetPassword</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 注册任何身份验证/授权服务</span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">boot</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword return-type">void</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token class-name static-context">ResetPassword</span><span class="token operator">::</span><span class="token function">createUrlUsing</span><span class="token punctuation">(</span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">User</span> <span class="token variable">$user</span><span class="token punctuation">,</span> <span class="token keyword type-hint">string</span> <span class="token variable">$token</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">        <span class="token keyword">return</span> <span class="token string single-quoted-string">'https://example.com/reset-password?token='</span><span class="token operator">.</span><span class="token variable">$token</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="重置邮件自定义" tabindex="-1"><a class="header-anchor" href="#重置邮件自定义"><span>重置邮件自定义</span></a></h4>
<p>你可以轻松修改用于向用户发送密码重置链接的通知类。 首先，覆盖你的 <code v-pre>App\Models\User</code> 模型上的 <code v-pre>sendPasswordResetNotification</code> 方法。 在此方法中，你可以使用你自己创建的任何 <a href="https://learnku.com/docs/laravel/10.x/notifications" target="_blank" rel="noopener noreferrer">通知类</a> 发送通知。 密码重置 <code v-pre>$token</code> 是该方法收到的第一个参数。 你可以使用这个 <code v-pre>$token</code> 来构建你选择的密码重置 URL 并将你的通知发送给用户：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">App<span class="token punctuation">\</span>Notifications<span class="token punctuation">\</span>ResetPasswordNotification</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 发送密码重置通知给用户</span>
<span class="line"> *</span>
<span class="line"> * <span class="token keyword">@param</span> <span class="token class-name"><span class="token keyword">string</span></span> <span class="token parameter">$token</span></span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">sendPasswordResetNotification</span><span class="token punctuation">(</span><span class="token variable">$token</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword return-type">void</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token variable">$url</span> <span class="token operator">=</span> <span class="token string single-quoted-string">'https://example.com/reset-password?token='</span><span class="token operator">.</span><span class="token variable">$token</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$this</span><span class="token operator">-></span><span class="token function">notify</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">ResetPasswordNotification</span><span class="token punctuation">(</span><span class="token variable">$url</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><blockquote>
<p>本译文仅用于学习和交流目的，转载请务必注明文章译者、出处、和本文链接<br>
我们的翻译工作遵照 <a href="https://learnku.com/docs/guide/cc4.0/6589" target="_blank" rel="noopener noreferrer">CC 协议</a>，如果我们的工作有侵犯到您的权益，请及时联系我们。</p>
</blockquote>
<hr>
<blockquote>
<p>原文地址：<a href="https://learnku.com/docs/laravel/10.x/passwords/14881" target="_blank" rel="noopener noreferrer">https://learnku.com/docs/laravel/10.x/pa...</a></p>
<p>译文地址：<a href="https://learnku.com/docs/laravel/10.x/passwords/14881" target="_blank" rel="noopener noreferrer">https://learnku.com/docs/laravel/10.x/pa...</a></p>
</blockquote>
</div></template>


